﻿namespace Admin.NET.Application.Service;

/// <summary>
/// 输血申请交叉配血主表
/// </summary>
public partial class TraPeiXueService : BaseServiceBill<TraPeiXue>
{
    private readonly SqlSugarRepository<TraPeiXueMingXi> _traShenQingPeiXueZiBiao;
    private readonly SqlSugarRepository<InvKuCun> _invKuCunBiao;

    public TraPeiXueService(
        IServiceScopeFactory scopeFactory,
        SqlSugarRepository<TraPeiXueMingXi> traShenQingPeiXueZiBiao,
        SqlSugarRepository<InvKuCun> invKuCunBiao,
        BillNoManager billNoManager
    )
        : base(scopeFactory)
    {
        _traShenQingPeiXueZiBiao = traShenQingPeiXueZiBiao;
        _invKuCunBiao = invKuCunBiao;
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<TraPeiXueOutput>> Page(BaseKeywordPageInput input)
    {
        var traShenQingPeiXueZhuBiao = await Queryable
            .FuzzyQuery(input.Keyword, new List<string> { "id" })
            .Select<TraPeiXueOutput>()
            .OrderByDescending(it => it.Id)
            .ToPagedListAsync(input.Page, input.PageSize);

        return traShenQingPeiXueZhuBiao;
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public async Task<TraShenQingPeiXueAllOutDto> Get(string id)
    {
        var traShenQingPeiXueZhuBiaoInput = GetById<TraPeiXueOutput>(id);
        var traShenQingPeiXueZiBiaoList = await GetZiBiao(id);
        return new TraShenQingPeiXueAllOutDto
        {
            traShenQingPeiXueZhuBiaoInput = traShenQingPeiXueZhuBiaoInput,
            traShenQingPeiXueZiBiaoList = traShenQingPeiXueZiBiaoList,
        };
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public TraPeiXue Add(TraPeiXueAllDto input)
    {
        input.traShenQingPeiXueZhuBiaoInput.danJuZhuangTai = DanJuZhuangTai.已生效;
        input.traShenQingPeiXueZhuBiaoInput.danJuLeiXing = "交叉配血";

        var entity = AddWithInput(input.traShenQingPeiXueZhuBiaoInput);

        AddWithZiBiaoInput(input.traShenQingPeiXueZiBiaoList, entity.Id);

        return entity;
    }

    /// <summary>
    /// 更新配血
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public bool Update(TraPeiXueAllDto input)
    {
        var entity = UpdateWithInput(input.traShenQingPeiXueZhuBiaoInput);

        UpdateWithZiBiaoInput(
            input.traShenQingPeiXueZiBiaoList,
            input.traShenQingPeiXueZhuBiaoInput.Id
        );

        return entity;
    }

    /// <summary>
    /// 配血血液是否已存在
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<List<string>> GetPeiXueBloodExists(long id)
    {
        var details = await _traShenQingPeiXueZiBiao
            .AsQueryable()
            .Where(a => a.xueYeId == id)
            .Select(d => d.traPeiXueId)
            .ToListAsync();
        var billNos = await Queryable
            .Where(m => details.Contains(m.Id) && m.danJuZhuangTai == DanJuZhuangTai.已生效)
            .Select(m => m.Id)
            .ToListAsync();
        return billNos;
    }
}
